package abc.demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-28
 * Time: 11:22
 */

public class WaitNotify {
    //    a     1     3
    //    b     2     2
    //    c     3     1
    //                  打印内容    等待标记    下一个等待标记
    public void print(String str,int waitFlag,int nextFlag) {
        for(int i =0;i<this.loopNumber;++i) {
            synchronized (this) {
                while(this.flag != waitFlag) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(str);
                flag = nextFlag;
                this.notifyAll();
            }
        }
    }

    //当前线程等待的标记
    private int flag;
    //循环次数
    private int loopNumber;

    public WaitNotify(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }
}
